import requests
import sqlite3
import re
import csv

cid=input('请输入数字：dx2，qt6，hs7，mt3，yz4，dzh5：')
name={'2':'dx','3':'mt','4':'yz','5':'dzh','6':'qt','7':'hs'}

def write_mysql(item,link):
    conn = sqlite3.connect('imgdb.sqlite3')
    cursor = conn.cursor()
    cursor.execute('insert into dbmv (item,link) values ("{}","{}")'.format(item,link))
    conn.commit()
    conn.close
for num in range(1,1000):
    item=name[cid]
    img_url_list=[]
    html_url='https://www.dbmeinv.com/dbgroup/show.htm?cid={}&pager_offset={}'.format(cid,num)
    response = requests.get(html_url)
    if response.status_code==404:
        print("第{}个页面不存在".format(num))
        continue
    html = (response.text)
    page_url_info = re.findall(r'a href="(.*?)" class="link" target="', html)
    for page_url in page_url_info:
        response = requests.get(page_url)
        if response.status_code != 200:
            print("加载错误")
            continue
        html = (response.text)
        img_url_info = re.findall(r'img src="(.*.jpg)"', html)
        for img_url in img_url_info:
                if img_url not in img_url_list:
                    img_url_list.append(img_url)
                    write_mysql(item,img_url)